package com.runfast.waimai.web.controller.Driver_controller;


import com.runfast.paotui.dao.model.RunfastLoginRecord;
import com.runfast.paotui.dao.model.RunfastShopper;
import com.runfast.paotui.service.RunfastShopperService;
import com.runfast.waimai.service.RunfastBusinessService;
import com.runfast.waimai.service.RunfastGoodsSellRecordService;
import com.runfast.waimai.util.MyCustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping("/driver/orderStatistics/*")
public class DriverOrderStatisticsController extends DriverBaseController{
	// 订单统计
	@Resource
	private RunfastGoodsSellRecordService goodsSellRecordService;
	////外卖商品订单RunfastGoodsSellRecord
	@Resource
	RunfastShopperService shopperService;
	@Resource
	private RunfastBusinessService businessService;
	//RunfastBusiness商家信息
	@InitBinder
	public void initBinder(WebDataBinder binder) {
		//注册转换器
		binder.registerCustomEditor(Date.class, new MyCustomDateEditor());
		//binder.registerCustomEditor(DateTime.class, new MyCustomDateEditor());
	}


	//订单统计  测试的时候骑手不是登陸状态，要给他设置Id
	@RequestMapping(value="shoplist",method=RequestMethod.POST)//验证
	public @ResponseBody ModelMap shoplist(@RequestParam(value = "page" ,required = false,defaultValue = "0") int page,String endTime,
			@RequestParam(value="rows",required = false,defaultValue = "10") int rows,
			 String startTime,String token) throws ParseException {
	      //格式转化
		DateFormat sim = new SimpleDateFormat("yyyy-MM-dd");
		Date starttime=sim.parse(startTime);
		Date endtime=sim.parse(endTime);
		ModelMap model = new ModelMap();
		RunfastShopper shopper  = super.getLoginShopper(token);
		Integer page1=page*rows;
		if(shopper!=null&&shopper.getAgentId()!=null){
		model.put("order",goodsSellRecordService.findBytime(starttime, endtime, shopper.getId(), page1, rows,shopper.getAgentId()));

			Integer total  = businessService.findtotal();//// 总页数
		     if(total!=null){
		    	 int  totalPage = total/rows;
				    model.put("totalPage", totalPage);//当前页面显示的条数
		     }else{
		    	   model.put("totalPage", 0);
		     }
			    
		}else {
			model.put("msg","骑手不能为空");
	//		model.put("shopperid",shopper.getId());
		}
		return model;
	}
}
